package com.yunxin.core.infra.persistence.repository.product;

import com.yunxin.core.domain.model.product.ProductInfo;
import com.yunxin.core.domain.model.product.ProductInfoRepository;
import com.yunxin.core.infra.persistence.converter.ProductConverter;
import com.yunxin.core.infra.persistence.entity.product.ProductInfoDO;
import com.yunxin.core.infra.persistence.entity.product.query.result.ProductSaveResult;
import com.yunxin.core.infra.persistence.mapper.product.ProductInfoExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author zero°
 * @since 2023-07-13
 */
@Repository
@RequiredArgsConstructor
public class ProductInfoRepositoryImpl implements ProductInfoRepository {

    private final ProductInfoExtendMapper productInfoExtendMapper;


    private final ProductConverter productConverter;

    @Override
    public boolean saveProductInfo(ProductInfo productInfo) {
        ProductInfoDO productInfoDO = productConverter.formProductInfo(productInfo);
        int count = productInfoExtendMapper.insertSelective(productInfoDO);
        productInfo.setId(productInfoDO.getId());
        return count > 0;
    }

    @Override
    public boolean updateProductInfo(ProductInfo productInfo) {
        ProductInfoDO productInfoDO = productConverter.formProductInfo(productInfo);
        int count = productInfoExtendMapper.updateByPrimaryKeySelective(productInfoDO);
        return count > 0;
    }

    @Override
    public Long getProductIdByMerchantIdAndProductName(Long merchantId, String productName) {
        return productInfoExtendMapper.selectProductIdByMerchantIdAndProductName(merchantId, productName);
    }

    @Override
    public ProductInfo getProductInfoById(Long id) {
        ProductInfoDO productInfoDO = productInfoExtendMapper.selectByPrimaryKey(id);
        return productConverter.toProductInfo(productInfoDO);
    }

    @Override
    public boolean getProductSaveResult(Long id) {
        ProductSaveResult result = productInfoExtendMapper.getProductSaveResult(id);
        return Objects.isNull(result.getProductCustomId()) ||
                Objects.isNull(result.getProductUnitPriceId()) || Objects.isNull(result.getProductThrowInId());
    }

    @Override
    public boolean updateProductProhibitStatus(ProductInfo updateProductInfoDO) {
        int count = productInfoExtendMapper.updateProductProhibitStatus(updateProductInfoDO);
        return count > 0;
    }
}
